package com.community.common.controller;


import com.community.common.pojo.domain.Picture;
import com.community.common.pojo.response.CodeEnum;
import com.community.common.pojo.response.MyResponse;
import com.community.common.service.PictureService;
import com.community.common.util.CommonUtil;
import com.community.common.util.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author superman
 * @since 2020-08-02
 */
@Slf4j
@Controller
@RequestMapping("/picture")
public class PictureController {

    @Resource
    private PictureService pictureService;

    /**
     * 图片上传
     */
    @PostMapping(value = "uploadImg/{pictureId}")
    @ResponseBody
    public MyResponse uploadFileOss(@RequestParam("file") MultipartFile file, HttpServletRequest request, @PathVariable Long pictureId) {
        if (file.isEmpty() || file.getOriginalFilename() == null) {
            log.error("uploadFile 上传的内容为空 getOriginalFilename不可为空 ");
            return MyResponse.fail("图片不能为空");
        }
        Picture picture = null;
        //当 pictureId=1234 时认为是个新用户
        if (pictureId != null && pictureId != 1234) {
            picture = pictureService.getById(pictureId);
            if (picture == null) {
                return MyResponse.fail(CodeEnum.NOT_FOUND);
            }
            OssUtil.delFile(CommonUtil.getOssRecordFileNameFromUrl(picture.getUrl()));
        }
        long id = CommonUtil.getId();
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String fileName = id + "." + suffix;
        boolean res = false;
        try {
            res = OssUtil.uploadFileByInputStream(file.getInputStream(), CommonUtil.getOssFileName(fileName));
        } catch (IOException e) {
            log.info("uploadFileOss file.getInputStream error ", e);
        }
        if (!res) {
            log.error("图片保存失败");
            return MyResponse.fail("图片保存失败");
        }

        // 保存图片信息
        String picUrl = CommonUtil.getOssFileUrl(fileName);
        boolean result;
        log.info("上传后的图片访问地址：{}", picUrl);
        if (pictureId != null && pictureId != 1234) {
            picture.setUpdateTime(LocalDateTime.now());
            picture.setUrl(picUrl);
            result = pictureService.updateById(picture);
        } else {
            picture = new Picture();
            picture.setId(String.valueOf(id));
            picture.setUrl(picUrl);
            picture.setCreateTime(LocalDateTime.now());
            picture.setUpdateTime(LocalDateTime.now());
            result = pictureService.save(picture);
        }
        if (result) {
            log.info("图片信息保存成功");
            return MyResponse.success(picture);
        } else {
            log.error("图片保存失败");
            return MyResponse.fail("图片保存失败");
        }
    }

}
